package com.one.common;

import android.app.Application;

import com.alibaba.android.arouter.launcher.ARouter;
import com.one.common.callback.WalletListener;
import com.one.common.manager.thread.ThreadPoolManager;
import com.one.common.model.http.NetConstant;
import com.one.common.receiver.fix.LoadedApkHuaWei;
import com.one.common.utils.CrashHandler;
import com.one.common.utils.Logger;
import com.one.common.utils.Toaster;

import cn.jpush.android.api.JPushInterface;

/**
 * creator: zhulunjun
 * time:    2018/8/3
 * describe:
 */
public class CommonApp extends Application {

    private static Application sApp;
    private static WalletListener walletListener;

    public static Application getInstance() {
        return sApp;
    }

    public static boolean isDebug = true;
    public static int config = 1;
    public static void init(Application app) {
        sApp = app;
        Toaster.init();
//        ARouter.openLog();     // 打印日志
        // 开启调试模式(如果在InstantRun模式下运行，必须开启调试模式！线上版本需要关闭,否则有安全风险)
        ARouter.init(sApp);   // 尽可能早，推荐在Application中初始化
        ThreadPoolManager.newInstance().perpare();
        JPushInterface.init(sApp);

        // 设置钱包环境
        if (NetConstant.ENVIRONMENT.equals("release") || NetConstant.ENVIRONMENT.equals("bastion")){
            initConfig(PRODUCT);
        }else {
            initConfig(DEV);
        }

        JPushInterface.setDebugMode(isDebug);
        Logger.setDebug(isDebug);
        if (isDebug) {
            ARouter.openDebug();
        }

        CrashHandler.getInstance().init(sApp);

        LoadedApkHuaWei.hookHuaWeiVerifier(app);

    }


    public static void setWalletListener(WalletListener listener) {
        walletListener = listener;
    }

    public static WalletListener getWalletListener() {
        return walletListener;
    }

    /**
     * 环境
     */
    private static final int TEST = 1;
    private static final int DEV = 0;
    private static final int BAOLEI = 2;
    private static final int PRODUCT = 3;
    private static void  initConfig(int config){
        switch (config){
            case DEV:
            case TEST:
            case BAOLEI:
                isDebug = true;
                break;
            case PRODUCT:
                isDebug = false;
                break;
        }
        CommonApp.config = config;
    }
}
